// 1. 基础数据类型
let username: string = "郑苗苗";
let age: number = 20;
let isStudent1: boolean = true;
let undefinedVar: undefined = undefined;
let nullVar: null = null;

// 2. 数组类型
let skills: string[] = ["Vue3", "Spring Boot", "MySQL"];
let scores: number[] = [95, 98, 92];
let mixedArray: (string | number)[] = ["前端", 95, "后端", 98];

// 3. 元组 - 固定长度和类型的数组
let studentInfo: [string, number, boolean] = ["郑苗苗", 20, true];

// 4. 枚举 - 提高代码可读性
enum ProjectRole {
    Frontend = "前端开发",
    Backend = "后端开发",
    FullStack = "全栈开发"
}

let myRole: ProjectRole = ProjectRole.FullStack;

// 5. 任意类型 - 谨慎使用
let dynamicData: any = "可以是任意类型";
dynamicData = 123;
dynamicData = true;

// 6. 空类型 - 常用于函数返回值
function logMessage(message: string): void {
    console.log(message);
}

// 7. 对象类型
interface Student {
    name: string;
    age: number;
    major: string;
    skills?: string[]; // 可选属性
}

let myProfile: Student = {
    name: "郑苗苗",
    age: 20,
    major: "软件技术",
    skills: ["Vue3", "Spring Boot", "MySQL"]
};

// 8. 类型别名
type CourseScore = {
    courseName: string;
    score: number;
    passed: boolean;
};

let myScores: CourseScore[] = [
    { courseName: "Java程序设计", score: 95, passed: true },
    { courseName: "数据库原理", score: 98, passed: true }
];

// 9. 联合类型
let id: string | number;
id = "STU2024001";
id = 2024001;

// 10. 函数类型
type Calculator = (a: number, b: number) => number;

const add: Calculator = (a, b) => a + b;
const multiply: Calculator = (a, b) => a * b;

// 11. 泛型 - 提高代码复用性
function identity<T>(arg: T): T {
    return arg;
}

let output1 = identity<string>("全栈开发");
let output2 = identity<number>(100);

// 12. 实际应用示例 - 项目数据建模
interface Project {
    id: number;
    name: string;
    techStack: string[];
    status: "进行中" | "已完成" | "已上线";
    teamMembers: Student[];
}

const myProject: Project = {
    id: 1,
    name: "眼镜店销售决策大屏",
    techStack: ["Vue3", "ECharts", "Spring Boot"],
    status: "已上线",
    teamMembers: [
        {
            name: "郑苗苗",
            age: 20,
            major: "软件技术",
            skills: ["Vue3", "数据可视化"]
        }
    ]
};

// 13. 类型守卫
function isStudent(obj: any): obj is Student {
    return obj && typeof obj.name === "string" && typeof obj.age === "number";
}

// 演示函数
function demonstrateTypes(): void {
    console.log("=== TypeScript 数据类型演示 ===");

    // 基础类型演示
    console.log("姓名:", username);
    console.log("年龄:", age);
    console.log("是否学生:", isStudent);

    // 数组和元组演示
    console.log("技术栈:", skills.join(", "));
    console.log("学生信息:", studentInfo);

    // 枚举演示
    console.log("项目角色:", myRole);

    // 对象演示
    console.log("个人档案:", myProfile);

    // 函数演示
    console.log("10 + 20 =", add(10, 20));
    console.log("10 * 20 =", multiply(10, 20));

    // 项目数据演示
    console.log("项目信息:", myProject);

    // 类型守卫演示
    if (isStudent(myProfile)) {
        console.log(`${myProfile.name} 是学生`);
    }
}

// 运行演示
demonstrateTypes();

// 14. 高级类型 - 映射类型
type ReadonlyStudent = {
    readonly [K in keyof Student]: Student[K];
};

// 15. 工具类型示例
type PartialStudent = Partial<Student>; // 所有属性变为可选
type RequiredStudent = Required<Student>; // 所有属性变为必选

// 导出供其他模块使用
export {
    Student,
    Project,
    ProjectRole,
    demonstrateTypes
};